#include <jni.h>
#include <android/log.h>
#include <stdio.h>
#include "method.h"

#define TAG "METHOD_JNI"
#define log(...) __android_log_print(ANDROID_LOG_WARN,TAG,__VA_ARGS__)

static jmethodID method_sum;
static jmethodID method_getArrayLength;
static jmethodID method_plus;
static jmethodID method_load;
static jmethodID method_play;
static jmethodID method_stop;

#define SUM_NUM 10
#define MUSIC_PATH "gee.mp3"

int sum_array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

void basic_sum(JNIEnv *env, jobject obj) {
	jclass cls = (*env)->GetObjectClass(env, obj);
	if (method_sum == NULL) {
		method_sum = (*env)->GetMethodID(env, cls, "sum", "([I)I");
	}
	jintArray array = (*env)->NewIntArray(env, SUM_NUM);
	(*env)->SetIntArrayRegion(env, array, 0, SUM_NUM, sum_array);
	int sum = (*env)->CallIntMethod(env, obj, method_sum, array);
	log("JNI sum 1 -10 : %d", sum);
}

void basic_getArrayLength(JNIEnv *env, jobject obj) {
	jclass cls = (*env)->GetObjectClass(env, obj);
	if (method_getArrayLength == NULL) {
		method_getArrayLength = (*env)->GetMethodID(env, cls, "getArrayLength",
				"([I)I");
	}
	jintArray array = (*env)->NewIntArray(env, SUM_NUM);
	(*env)->SetIntArrayRegion(env, array, 0, SUM_NUM, sum_array);
	jsize length = (*env)->CallIntMethod(env, obj, method_getArrayLength,
			array);
	log("JNI sum length : %d ", length);
}

void basic_plus(JNIEnv *env, jobject obj) {
	jclass cls = (*env)->GetObjectClass(env, obj);
	if (method_plus == NULL) {
		method_plus = (*env)->GetStaticMethodID(env, cls, "plus", "([I)I");
	}
	jintArray array = (*env)->NewIntArray(env, SUM_NUM);
	(*env)->SetIntArrayRegion(env, array, 0, SUM_NUM, sum_array);
	jlong plus = (*env)->CallStaticIntMethod(env, cls, method_plus, array);
	log("JNI plus : %d ", plus);
}

void basic_load(JNIEnv *env, jobject obj) {
	if (method_load == NULL) {
		jclass cls = (*env)->GetObjectClass(env, obj);
		method_load = (*env)->GetMethodID(env, cls, "loadSound",
				"(Ljava/lang/String;)V");
	}
	(*env)->CallVoidMethod(env, obj, method_load,
			(*env)->NewStringUTF(env, MUSIC_PATH));
}

void basic_play(JNIEnv *env, jobject obj) {
	if (method_play == NULL) {
		jclass cls = (*env)->GetObjectClass(env, obj);
		method_play = (*env)->GetMethodID(env, cls, "playSound", "()V");
	}
	(*env)->CallVoidMethod(env, obj, method_play);
}

void basic_stop(JNIEnv *env, jobject obj) {
	if (method_stop == NULL) {
		jclass cls = (*env)->GetObjectClass(env, obj);
		method_stop = (*env)->GetMethodID(env, cls, "stopSound", "()V");
	}
	(*env)->CallVoidMethod(env, obj, method_stop);
}

void basic_defaultConstructor(JNIEnv *env, jobject obj) {
	jclass cls = (*env)->FindClass(env, "com/ransj/ndkpath/basic/PageMethod$Method");
	jmethodID constructorID = (*env)->GetMethodID(env, cls, "<init>",
			"()V");
	jmethodID showID = (*env)->GetMethodID(env, cls, "showName",
			"(Lcom/ransj/ndkpath/basic/PageMethod;)V");
	jobject objMethod = (*env)->NewObject(env, cls, constructorID);
	(*env)->CallVoidMethod(env, objMethod, showID, obj);
}

void basic_constructorWithName(JNIEnv *env, jobject obj, jstring name) {
	jclass cls = (*env)->FindClass(env, "com/ransj/ndkpath/basic/PageMethod$Method");
	jmethodID constructorID = (*env)->GetMethodID(env, cls, "<init>",
			"(Ljava/lang/String;)V");
	jmethodID showID = (*env)->GetMethodID(env, cls, "showName",
			"(Lcom/ransj/ndkpath/basic/PageMethod;)V");
	jobject objMethod = (*env)->NewObject(env, cls, constructorID, name);
	(*env)->CallVoidMethod(env, objMethod, showID, obj);
}
